#!/bin/bash
#
# @@@ START COPYRIGHT @@@
#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied.  See the License for the
# specific language governing permissions and limitations
# under the License.
#
# @@@ END COPYRIGHT @@@

# This script cleans up the log files of various components
if [ -e $SQ_PDSH ]; then
   L_PDSH="/usr/bin/pdsh $MY_NODES "
else
   L_PDSH=
fi

function usage() {
   prog=`basename $0` 
   echo ""
   echo "$prog { all | dcs | rest | core }"
   echo "    all  --- Remove files from core, dcs and rest logs folder"
   echo "    dcs  --- Remove files from $TRAF_LOG/dcs folder"
   echo "    rest --- Remove files from $TRAF_LOG/rest folder"
   echo "    core --- Remove log files residing in $TRAF_LOG folder"
}

function core_logs() {
 if [[ ! -z $L_PDSH ]]; then
   $L_PDSH "rm -f ${TRAF_LOG}/*.err"
   $L_PDSH "rm -f ${TRAF_LOG}/*.log"
   $L_PDSH "rm -f ${TRAF_LOG}/*log.[0-9]*"
   $L_PDSH "rm -f ${TRAF_LOG}/stdout_*"
 else
   rm -f ${TRAF_LOG}/*
 fi
}

function dcs_logs() {
 if [[ ! -z $L_PDSH ]]; then
   $L_PDSH "rm -f ${TRAF_LOG}/dcs/*.log"
   $L_PDSH "rm -f ${TRAF_LOG}/dcs/*.log.[0-9]*"
   $L_PDSH "rm -f ${TRAF_LOG}/dcs/*.out"
   $L_PDSH "rm -f ${TRAF_LOG}/dcs/*.out.[0-9]*"
 else
   rm -f ${TRAF_LOG}/dcs/*
 fi
}

function rest_logs() {
 if [[ ! -z $L_PDSH ]]; then
   $L_PDSH "rm -f ${TRAF_LOG}/rest/*.log"
   $L_PDSH "rm -f ${TRAF_LOG}/rest/*.log.[0-9]*"
   $L_PDSH "rm -f ${TRAF_LOG}/rest/*.out"
   $L_PDSH "rm -f ${TRAF_LOG}/rest/*.out.[0-9]*"
 else
   rm -f ${TRAF_LOG}/rest/*
 fi
}

function all_logs() {
   core_logs
   dcs_logs
   rest_logs
}

if [ ! -z ${TRAF_HOME} ]; then
   case "$1" in 
      core)
        core_logs
        ;;
      dcs)
        dcs_logs
        ;;
      rest)
        rest_logs
        ;;
      all)
        all_logs
        ;;
      *)
        usage
        exit 1
   esac
fi
exit 0
